#!/bin/bash

if [ ! -d /vagrant ] ; then
  echo "This should only be run inside the vagrant VM, nerds."
  exit 2
fi

if [ -f /usr/local/zeus/zxtm/bin/zconf ] ; then
  echo "Looks like stingray is already installed.  Buh-bye."
  exit 0
fi

apt-get update
apt-get install -y vim-nox curl

cat >/usr/local/bin/stingray-primaryip <<EOF
#!/bin/sh
exec ifconfig eth1 | awk -F: '/inet addr/ { gsub(/[^0-9\.]/, "", \$2) ; print \$2 }'
EOF
chmod 755 /usr/local/bin/stingray-primaryip


set -e
set -x

pushd /tmp
tar xzvf /vagrant/ZeusTM_91_Linux-x86_64.tgz
pushd ZeusTM_91_Linux-x86_64
./zinstall \
  --noninteractive \
  --replay-from=/vagrant/stingray_install.txt \
  --quiet >./install.stdout 2>./install.stderr
/usr/local/zeus/zxtm/configure \
  --noninteractive \
  --replay-from=/vagrant/stingray_configure.txt \
  --quiet >./configure.stdout 2>./configure.stderr

set +x
echo
echo "--------------------------------------------------"
echo "---- Stingray install all done.  Don't panic. ----"
echo "--------------------------------------------------"
echo
echo "----------------------------------------------------------------"
echo "---- Sadly, you'll still have to go here and click on the   ----"
echo "---- 'developer mode' button:                               ----"
echo "---- https://$(stingray-primaryip):9090/apps/sxtm/login.cgi ----"
echo "----------------------------------------------------------------"
echo
exit 0
